import * as app from '../../common/utils/BaseApp'
import { deepClone, getDictName } from '@/utils'
import * as ck from '../../common/utils/validater'
import { hasActionPriv } from '@/utils/perm'
import * as elg from '../../common/utils/ElmGenerator'

import { getJobTitleList, createJobTitle, updateJobTitle, deleteJobTitle } from '@/api/system/job.title'
import langFile from './jobtitle.lang'
const base = app.install({ langObj: langFile })
const pageMode = {
  add: 'add',
  detailAdd: 'detailAdd',
  update: 'update'
}
export default {
  mixins: [base],
  data() {
    return {
      formObj: {
        searchForm: {
          // ----------------------- 岗位查询条件 ---------------------
          jobTitle: {
            jobtitleId: '',
            jobtitleName: '',
            status: ''
          },
          likeSearch: {
            jobTitle: ['jobtitleId', 'jobtitleName']
          }
        },
        addForm: {
          // ----------------------- 岗位Form结构 ---------------------
          jobTitle: {
            id: undefined,
            jobtitleId: '',
            jobtitleName: '',
            sortOrder: '',
            status: '1',
            memo: ''
          },
          showFlg: {
            jobTitle: false
          },
          showMode: {
            jobTitle: ''
          }
        }
      },
      listObj: {
        // ----------------------- 岗位列表 ---------------------
        jobTitle: {
          ...base.$gcom.getDefListObj(),
          selectedData: []
        }
      }
    }
  },
  computed: {
    isAddJobTitle() {
      return this.formObj.addForm.showMode.jobTitle === pageMode.add ||
             this.formObj.addForm.showMode.jobTitle === pageMode.detailAdd
    },
    // ----------------------- 岗位查询条件Layout ---------------------
    jobTitleSearchFormLayout() {
      const formItems = [
        elg.createInput(this, { bindId: 'jobtitleId', propsOpt: { width: 180 }}),
        elg.createInput(this, { bindId: 'jobtitleName', propsOpt: { width: 180 }}),
        elg.createSelect(this, { bindId: 'status',
          optionList: elg.createChildOption(this.$dictObj['sys_valid_flg'].dict), propsOpt: { width: 160 }}),

        elg.createButton(this, this.handleSearchJobTitle, { langId: 'search', actionValid: true, propsOpt: { icon: 'el-icon-search' }}),
        elg.createBR(),
        hasActionPriv(['sys:job:add']) === false ? null
          : elg.createButton(this, this.handleAddJobTitle, { langId: 'add', propsOpt: { icon: 'el-icon-edit' }})
      ]
      const rules = {
        jobtitleId: [ck.createMaxLenValidater(this, 20)],
        jobtitleName: [ck.createMaxLenValidater(this, 30)]
      }
      return { formItems: formItems, rules: rules }
    },
    // ----------------------- 岗位新增/修改Form Layout ---------------------
    jobTitleAddFormLayout() {
      const formItems = [
        elg.createInput(this, { bindId: 'jobtitleId', propsOpt: { width: 180 }}),
        elg.createInput(this, { bindId: 'jobtitleName', propsOpt: { width: 180 }}),
        elg.createInputNumber(this, { bindId: 'sortOrder' }),
        elg.createSelect(this, { bindId: 'status',
          optionList: elg.createChildOption(this.$dictObj['sys_valid_flg'].dict), propsOpt: { width: 160 }}),
        elg.createTextArea(this, { bindId: 'memo' })
      ]
      const footerItems = [
        hasActionPriv(['sys:job:add', 'sys:job:update']) === false ? null
          : elg.createButton(this, this.handleSaveJobTitle, { langId: 'confirm', actionValid: true, propsOpt: { icon: '' }})
      ]

      const rules = {
        jobtitleId: [
          ck.createRequiredValidater(this), ck.createMaxLenValidater(this, 20)
        ],
        jobtitleName: [
          ck.createRequiredValidater(this), ck.createMaxLenValidater(this, 30)
        ],
        sortOrder: [
          ck.createRequiredValidater(this)
        ],
        status: [
          ck.createRequiredValidater(this)
        ],
        memo: [
          ck.createMaxLenValidater(this, 255)
        ]
      }
      return { formItems: formItems, footerItems: footerItems, rules: rules }
    },
    // ----------------------- 岗位列表 Layout ---------------------
    jobtitleListLayout() {
      const itemColumns = [
        elg.createTableColumn(this, { bindId: 'jobtitleId', align: 'left' }),
        elg.createTableColumn(this, { bindId: 'jobtitleName', align: 'center' }),
        elg.createTableColumn(this, { bindId: 'status',
          formatter: {
            formatFnc: (row, v) => {
              return getDictName(this.$dictObj['sys_valid_flg'].dict, v)
            }
          }, align: 'center' }),
        elg.createTableColumn(this, { bindId: 'sortOrder', align: 'center' }),
        elg.createTableColumn(this, { bindId: 'memo', align: 'center' })
      ]
      const actionColumns = [
        hasActionPriv(['sys:job:update']) === false ? null
          : elg.createTableAction(this, { langId: 'edit', actionFnc: this.handleUpdateJobtitle }),
        hasActionPriv(['sys:job:delete']) === false ? null
          : elg.createTableAction(this, { langId: 'delete', actionFnc: this.handleDeleteJobTitle })
      ]
      return { columns: itemColumns, actions: actionColumns }
    }
  },
  created() {
    this.$gcom.formObj = deepClone(this.formObj)
    this.searchJobTitleList()
  },
  methods: {
    // ----------------------- 岗位列表查询 ---------------------
    async searchJobTitleList() {
      const _target = this.listObj.jobTitle
      try {
        _target.listLineLoading = true

        const result = await getJobTitleList({
          searchForm: this.formObj.searchForm.jobTitle,
          likeSearch: this.formObj.searchForm.likeSearch.jobTitle,
          pagingDefine: this.listObj.jobTitle.pagingDefine
        })

        _target.listLine = result.data.listLine
        _target.lineNum = result.data.lineNum
      } catch (err) {
        console.log(err)
      } finally {
        setTimeout(() => {
          _target.listLineLoading = false
        }, 0)
      }
    },
    handleSearchJobTitle() {
      this.validateAll(['searchForm'], () => {
        this.listObj.jobTitle.pagingDefine.page = 1
        this.searchJobTitleList()
      })
    },
    async handleAddJobTitle(row, index) {
      this.openDialog('jobTitle', pageMode.add)
    },
    async handleUpdateJobtitle(row, index) {
      this.openDialog('jobTitle', pageMode.update, row)
    },
    // ----------------------- 岗位信息保存 ---------------------
    handleSaveJobTitle() {
      this.validateAll(['jobTitleEditDlg'], async() => {
        const _form = { ... this.formObj.addForm.jobTitle }
        if (this.isAddJobTitle) {
          await createJobTitle(_form)
        } else {
          await updateJobTitle(_form)
        }
        this.listObj.jobTitle.pagingDefine.page = 1
        this.searchJobTitleList()
        this.formObj.addForm.showFlg.jobTitle = false
      })
    },
    // ----------------------- 岗位信息删除 ---------------------
    async handleDeleteJobTitle(row, index) {
      this.$showConfirm({ msg: this.getLang('message.rule.deleteJobConfirm'), ok: async() => {
        await deleteJobTitle(row.id)
        setTimeout(async() => {
          this.listObj.jobTitle.pagingDefine.page = 1
          this.searchJobTitleList()
        }, 500)
      } })
    },
    // ----------------------- Open Dialog ---------------------
    openDialog(target, mode, row, index) {
      const data = row === undefined ? this.$gcom.formObj.addForm[target] : row
      this.formObj.addForm[target] = { ...data }
      this.formObj.addForm.showFlg[target] = true
      this.formObj.addForm.showMode[target] = mode
    }
  }
}
